//
//  JsViewHIli.m
//  MpaClientIos
//
//  Created by Gikki Ares on 2023/4/21.
//  Copyright © 2023 Yach. All rights reserved.
//

#import "JsViewHIli.h"
#import "JsBundleManager.h"

@implementation JsViewHIliVm

+(instancetype)defaultVm {
	JsViewHIliVm * vm = [JsViewHIliVm new];
	vm.leftImageSize = CGSizeMake(30,30);
	vm.rightImageSize = CGSizeMake(20,30);
	vm.padding = UIEdgeInsetsMake(5,5,-5,-5);
	vm.titleLeading = 5;
	vm.leftImageLeading = 5;
	vm.rightImageTrailing = 5;
	vm.cornerRadius = 5;
	return vm;
}

@end


@interface JsViewHIli() {

	 IBOutlet UIImageView *mIv_left;
	 IBOutlet UIImageView *mIv_right;
	 IBOutlet UILabel *mlb;
	
	 IBOutlet UIView *mV_content;
	
	IBOutlet NSLayoutConstraint *mLc_leftImageLeading;
	IBOutlet NSLayoutConstraint *mLc_labelLeading;
	IBOutlet NSLayoutConstraint *mLc_leftImageWidth;
	IBOutlet NSLayoutConstraint *mLc_leftImageHeight;
	IBOutlet NSLayoutConstraint *mLc_rightImageWidth;
	IBOutlet NSLayoutConstraint *mLc_rightImageHeight;
	IBOutlet NSLayoutConstraint *mLc_rightImageTrailing;
	
	IBOutlet NSLayoutConstraint *mLc_marginLeft;
	IBOutlet NSLayoutConstraint *mLc_marginBottom;
	IBOutlet NSLayoutConstraint *mLc_marginTop;
	IBOutlet NSLayoutConstraint *mLc_marginRight;
}
@end

@implementation JsViewHIli

- (instancetype)init {
	if(self = [super init]) {
		
	}
	return self;
}

- (instancetype)initWithFrame:(CGRect)frame{
	if(self = [super initWithFrame:frame]) {
		
	}
	return self;
}

- (instancetype)initWithVm:(JsViewVm *)vm {
	if(self = [super initWithVm:vm]) {
		//需要添加约束.和子视图.
		mV_content = [[UIView alloc]init];
		mV_content.translatesAutoresizingMaskIntoConstraints = NO;
		
		[self addSubview:mV_content];
		self.autoresizingMask = UIViewAutoresizingNone;
		self.translatesAutoresizingMaskIntoConstraints = NO;

		
		//添加约束.
		mLc_marginLeft = [mV_content.leadingAnchor constraintEqualToAnchor:self.leadingAnchor constant:self.vm.padding.left] ;
		mLc_marginLeft.active = YES;
		mLc_marginRight = [mV_content.trailingAnchor constraintEqualToAnchor:self.trailingAnchor constant:self.vm.padding.right];
		mLc_marginRight.active = YES;
		mLc_marginTop = [mV_content.topAnchor constraintEqualToAnchor:self.topAnchor constant:self.vm.padding.top];
		mLc_marginTop.active = YES;
		mLc_marginBottom = [mV_content.bottomAnchor constraintEqualToAnchor:self.bottomAnchor constant:self.vm.padding.bottom];
		mLc_marginBottom.active = YES;
	}
	return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder {
	if(self = [super initWithCoder:coder]) {
		NSString * className = NSStringFromClass([self class]);
		//本质上,是把xib中的view体系加载为self.subview.
		NSBundle * bundle = [JsBundleManager jsUtilityBundle];
		UIView * rootView = [[bundle loadNibNamed:className owner:self options:nil] firstObject];
		rootView.frame = self.bounds;
		[self addSubview:rootView];
		
	}
	return self;
}

- (UIImageView *)leftImageView {
	if(!mIv_left) {
		mIv_left = [UIImageView new];
		mIv_left.translatesAutoresizingMaskIntoConstraints = NO;
		[mV_content addSubview:mIv_left];
		mLc_leftImageWidth = [mIv_left.widthAnchor constraintEqualToConstant:self.vm.leftImageSize.width];
		mLc_leftImageWidth.active = YES;
		mLc_leftImageHeight = [mIv_left.heightAnchor constraintEqualToConstant:self.vm.leftImageSize.height];
		mLc_leftImageHeight.active = YES;
		mLc_leftImageLeading = [mIv_left.leadingAnchor constraintEqualToAnchor:mV_content.leadingAnchor constant:self.vm.leftImageLeading];
		mLc_leftImageLeading.active = YES;
		[mIv_left.centerYAnchor constraintEqualToAnchor:mV_content.centerYAnchor].active = YES;
		
	}
	return mIv_left;
}
- (UIImageView *)rightImageView {
	if(!mIv_right) {
		mIv_right = [UIImageView new];
		mIv_right.translatesAutoresizingMaskIntoConstraints = NO;
		[mV_content addSubview:mIv_right];
		mLc_rightImageWidth = [mIv_right.widthAnchor constraintEqualToConstant:self.vm.leftImageSize.width];
		mLc_rightImageWidth.active = YES;
		mLc_rightImageHeight = [mIv_right.heightAnchor constraintEqualToConstant:self.vm.leftImageSize.height];
		mLc_rightImageHeight.active = YES;
		mLc_rightImageTrailing = [mIv_right.leadingAnchor constraintEqualToAnchor:mV_content.leadingAnchor constant:self.vm.rightImageTrailing];
		[mIv_right.centerYAnchor constraintEqualToAnchor:mV_content.centerYAnchor].active = YES;
		mLc_rightImageTrailing.active = YES;
	}
	return mIv_right;
}

- (UILabel *)lbl {
	if(!mlb) {
		mlb = [UILabel new];
		mlb.translatesAutoresizingMaskIntoConstraints = NO;
		[mV_content addSubview:mlb];
		[mlb.centerYAnchor constraintEqualToAnchor:mV_content.centerYAnchor].active = YES;
		if(mIv_left) {
			mLc_labelLeading = [mlb.leadingAnchor constraintEqualToAnchor:mIv_left.trailingAnchor constant:self.vm.titleLeading];
			mLc_labelLeading.active = YES;
		}
	}
	return mlb;
}

- (void)awakeFromNib {
	[super awakeFromNib];
}
//作为barButtonItem时,代码设置大小无效.只能手动设置约束的方式来设置.
- (void)setFrame:(CGRect)frame {
	[super setFrame:frame];
}

- (void)renderVm {
	[super renderVm];
	if(self.vm.leftImage) {
		self.leftImageView.image = self.vm.leftImage;
		mLc_leftImageWidth.constant = self.vm.leftImageSize.width;
		mLc_leftImageHeight.constant = self.vm.leftImageSize.height;
		mLc_leftImageLeading.constant = self.vm.leftImageLeading;
	}
	if(self.vm.title) {
		self.lbl.text = self.vm.title;
		mLc_labelLeading.constant = self.vm.titleLeading;
	}
	if(self.vm.rightImage) {
		self.rightImageView.image = self.vm.rightImage;
		mLc_rightImageWidth.constant = self.vm.rightImageSize.width;
		mLc_rightImageHeight.constant = self.vm.rightImageSize.height;
		mLc_rightImageTrailing.constant = self.vm.rightImageTrailing;
	}
	
	if(self.vm.titleColor) {
		mlb.textColor = self.vm.titleColor;
	}
	else {
		mlb.textColor = [UIColor whiteColor];
	}
	
	
	if(self.vm.contentBackgroundColor) {
		mV_content.backgroundColor = self.vm.contentBackgroundColor;
	}
	
	if(self.vm.cornerRadius > 0) {
		mV_content.layer.cornerRadius = self.vm.cornerRadius;
		mV_content.layer.masksToBounds = YES;
	}
	if(self.vm.contentBackgroundColor) {
		mV_content.backgroundColor = self.vm.contentBackgroundColor;
	}

//	float x = self.frame.origin.x;
//	float y = self.frame.origin.y;
	

}



- (void)setVm:(JsViewHIliVm *)vm {
	[super setVm:vm];
//	mIv_left.image = vm.leftImage;
//	mIv_right.image = vm.rightImage;
//	mlb.text = vm.title;
//
//
//	mLc_leftImageLeading.constant = vm.leftImageLeading;
//	mLc_leftImageWidth.constant = vm.leftImageSize.width;
//	mLc_leftImageHeight.constant = vm.leftImageSize.height;
//
//	mLc_labelLeading.constant = vm.titleLeading;
//
//	mLc_rightImageWidth.constant = vm.rightImageSize.width;
//	mLc_rightImageHeight.constant = vm.rightImageSize.height;
//	mLc_rightImageTrailing.constant = vm.rightImageTrailing;
//
//	mlb.textColor = [UIColor whiteColor];
//
//	if(vm.contentBackgroundColor) {
//		mV_content.backgroundColor = vm.contentBackgroundColor;
//	}
//
//	if(vm.cornerRadius > 0) {
//		mV_content.layer.cornerRadius = vm.cornerRadius;
//		mV_content.layer.masksToBounds = YES;
//	}
}
@end
